﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Linq;
using System.Reflection;
using System.Windows.Forms;
using Assist;
using Autodesk.Revit.DB;
using YArchitech.Controls;
using YArchitech.DataModel;
using YArchitech.LIB;
using YArchitech.LIB.YJKControls;
using YArchitech.Revit;

namespace HYFloorPipeModule
{
	public partial class HYDrawDrpgForm : System.Windows.Forms.Form
	{
		public bool BeLinkSelect { get; set; }

		public HYDrawDrpgForm()
		{
			this.InitializeComponent();
			new YJKProcessTools().ShowInTaskbarChange(this);
			base.Icon = Resource.YArchitechIcon;
			double num = 1.0;
			double num2 = 1.0;
			WinDPIManager.GetWinFormXYDpi(ref num, ref num2);
			base.Size = new Size(Convert.ToInt32(565.0 * num), Convert.ToInt32(385.0 * num2));
		}

		public HYDrawDrpgForm(List<GraphicsStyle> gs, Dictionary<string, string> dicPara = null)
		{
			this.InitializeComponent();
			new YJKProcessTools().ShowInTaskbarChange(this);
			base.Icon = Resource.YArchitechIcon;
			this.dic = dicPara;
			this.LineStyle = gs;
			this.ReadUserUIData();
			this.InitShowUI();
		}

		private void InitShowUI()
		{
			this.InitCmb();
			this.cmbPgStyle.SelectedIndex = (int)this.drawFloorPipeData.PipeStyle;
			this.chkCenter.Checked = this.drawFloorPipeData.BDrawCenter;
			this.hytbDisA.Text = this.drawFloorPipeData.DisA.ToString();
			this.hytbDisB.Text = this.drawFloorPipeData.DisB.ToString();
			this.hytbDisC.Text = this.drawFloorPipeData.DisC.ToString();
			this.hytbDisD.Text = this.drawFloorPipeData.DisD.ToString();
			this.hytbDisToWall.Text = this.drawFloorPipeData.DisToWall.ToString();
			this.hytbRadius.Text = this.drawFloorPipeData.DisRadius.ToString();
			this.hytbBoundLeftDis.Text = this.drawFloorPipeData.BoundLeftDis.ToString();
			this.hytbBoundLeftWidth.Text = this.drawFloorPipeData.BoundLeftWidth.ToString();
			this.hytbBoundRightWidth.Text = this.drawFloorPipeData.BoundRightWidth.ToString();
			this.hytbBoundRightDis.Text = this.drawFloorPipeData.BoundRightDis.ToString();
			this.hytbPipeDN.Text = this.drawFloorPipeData.PipeDN.ToString();
			this.hytbPipeMaterial.Text = this.drawFloorPipeData.PipeMaterial.ToString();
			if (this.drawFloorPipeData.InType == null)
			{
				GraphicsStyle graphicsStyle = this.LineStyle.FirstOrDefault((GraphicsStyle s) => s.Name == "地热盘管供水");
				if (graphicsStyle == null)
				{
					this.cmbInLineStyle.SelectedIndex = 0;
					this.drawFloorPipeData.InType = this.LineStyle[0];
				}
				else
				{
					this.cmbInLineStyle.SelectedItem = graphicsStyle.Name;
					this.drawFloorPipeData.InType = graphicsStyle;
				}
			}
			else
			{
				this.cmbInLineStyle.SelectedItem = this.drawFloorPipeData.InType.Name;
			}
			if (this.drawFloorPipeData.OutType == null)
			{
				GraphicsStyle graphicsStyle2 = this.LineStyle.FirstOrDefault((GraphicsStyle s) => s.Name == "地热盘管回水");
				if (graphicsStyle2 == null)
				{
					this.cmbOutLineStyle.SelectedIndex = 1;
					this.drawFloorPipeData.OutType = this.LineStyle[1];
				}
				else
				{
					this.cmbOutLineStyle.SelectedItem = graphicsStyle2.Name;
					this.drawFloorPipeData.OutType = graphicsStyle2;
				}
			}
			else
			{
				this.cmbOutLineStyle.SelectedItem = this.drawFloorPipeData.OutType.Name;
			}
			this.cmbMateria.SelectedItem = this.drawFloorPipeData.PipeMaterial;
			this.LoadBmps();
			this.chbLayRoom.Checked = this.bLayInRoom;
			this.chbMarkTag.Checked = this.bMarkTag;
			if (this.chbLayRoom.Checked)
			{
				this.ckbLinkSelect.Enabled = true;
			}
			else
			{
				this.ckbLinkSelect.Enabled = false;
			}
			this.ckbLinkSelect.Checked = this.BeLinkSelect;
			if (this.dic != null)
			{
				double num = double.Parse(this.dic["distance"]);
				string selectedItem = this.dic["Material"];
				this.hytbDisA.Text = num.ToString();
				this.hytbDisB.Text = num.ToString();
				this.hytbDisC.Text = num.ToString();
				this.hytbDisD.Text = num.ToString();
				this.cmbMateria.SelectedItem = selectedItem;
				this.chbLayRoom.Checked = true;
			}
		}

		private void InitCmb()
		{
			this.cmbPgStyle.DataSource = this.sPipeType;
			List<string> list = new List<string>();
			foreach (GraphicsStyle graphicsStyle in this.LineStyle)
			{
				if (!list.Contains(graphicsStyle.Name))
				{
					list.Add(graphicsStyle.Name);
				}
			}
			this.cmbInLineStyle.DataSource = list;
			List<string> list2 = new List<string>();
			foreach (GraphicsStyle graphicsStyle2 in this.LineStyle)
			{
				if (!list2.Contains(graphicsStyle2.Name))
				{
					list2.Add(graphicsStyle2.Name);
				}
			}
			this.cmbOutLineStyle.DataSource = list2;
			this.cmbMateria.DataSource = this._strMaterial;
		}

		private void LoadBmps()
		{
			string text = this.cmbPgStyle.Text;
			this.drawFloorPipeData.OutType = this.LineStyle[1];
			this.drawFloorPipeData.InType = this.LineStyle[0];
			if (!(text == "回转"))
			{
				if (!(text == "直列"))
				{
					if (!(text == "往复"))
					{
						if (!(text == "跨越"))
						{
							if (text == "单回转")
							{
								this.pictureBox1.Image = Image.FromFile(Product.InstallLocation + "\\Res\\单回转.png");
								this.drawFloorPipeData.OutType = this.LineStyle[0];
								this.drawFloorPipeData.InType = this.LineStyle[0];
							}
						}
						else
						{
							this.pictureBox1.Image = Image.FromFile(Product.InstallLocation + "\\Res\\跨越.png");
						}
					}
					else
					{
						this.pictureBox1.Image = Image.FromFile(Product.InstallLocation + "\\Res\\往复.png");
					}
				}
				else
				{
					this.pictureBox1.Image = Image.FromFile(Product.InstallLocation + "\\Res\\直列.png");
				}
			}
			else
			{
				this.pictureBox1.Image = Image.FromFile(Product.InstallLocation + "\\Res\\回转.png");
			}
			int selectedIndex = this.cmbPgStyle.SelectedIndex;
			string name = Enum.GetName(typeof(ePipeType), selectedIndex);
			Dictionary<string, string> floorPipeDrawSetting = this.userSetting.GetFloorPipeDrawSetting(name.Remove(0, 1));
			foreach (PropertyInfo propertyInfo in this.drawFloorPipeData.GetType().GetProperties())
			{
				if (!(floorPipeDrawSetting[propertyInfo.Name] == "") && (propertyInfo.Name == "InType" || propertyInfo.Name == "OutType"))
				{
					propertyInfo.SetValue(this.drawFloorPipeData, this.GetLineStyleByNames(floorPipeDrawSetting[propertyInfo.Name]));
				}
			}
		}

		private GraphicsStyle GetLineStyleByNames(string name)
		{
			GraphicsStyle result = null;
			foreach (GraphicsStyle graphicsStyle in this.LineStyle)
			{
				if (name == graphicsStyle.Name)
				{
					result = graphicsStyle;
					break;
				}
			}
			return result;
		}

		private void ReadUIData()
		{
			this.drawFloorPipeData.PipeStyle = (FloorPipeStyle)this.cmbPgStyle.SelectedIndex;
			this.drawFloorPipeData.DisToWall = double.Parse(this.hytbDisToWall.Text) / 304.8;
			this.drawFloorPipeData.DisA = double.Parse(this.hytbDisA.Text) / 304.8;
			this.drawFloorPipeData.DisB = double.Parse(this.hytbDisB.Text) / 304.8;
			this.drawFloorPipeData.DisC = double.Parse(this.hytbDisC.Text) / 304.8;
			this.drawFloorPipeData.DisD = double.Parse(this.hytbDisD.Text) / 304.8;
			this.drawFloorPipeData.DisRadius = double.Parse(this.hytbRadius.Text) / 304.8;
			this.drawFloorPipeData.BoundLeftDis = double.Parse(this.hytbBoundLeftDis.Text) / 304.8;
			this.drawFloorPipeData.BoundLeftWidth = double.Parse(this.hytbBoundLeftWidth.Text) / 304.8;
			this.drawFloorPipeData.BoundRightWidth = double.Parse(this.hytbBoundRightWidth.Text) / 304.8;
			this.drawFloorPipeData.BoundRightDis = double.Parse(this.hytbBoundRightDis.Text) / 304.8;
			this.drawFloorPipeData.PipeDN = double.Parse(this.hytbPipeDN.Text) / 304.8;
			this.drawFloorPipeData.PipeMaterial = this.cmbMateria.Text;
			this.drawFloorPipeData.BDrawCenter = this.chkCenter.Checked;
			this.bLayInRoom = this.chbLayRoom.Checked;
			this.bMarkTag = this.chbMarkTag.Checked;
			if (this.bLayInRoom)
			{
				this.BeLinkSelect = this.ckbLinkSelect.Checked;
				return;
			}
			this.BeLinkSelect = false;
		}

		private void UIContrlsEanble(int typeIndex)
		{
			switch (typeIndex)
			{
			case 0:
			case 4:
				this.hytbDisB.Enabled = true;
				this.hytbDisC.Enabled = true;
				this.hytbDisD.Enabled = true;
				this.hytbBoundLeftDis.Enabled = false;
				this.hytbBoundLeftWidth.Enabled = false;
				this.hytbBoundRightDis.Enabled = false;
				this.hytbBoundRightWidth.Enabled = false;
				this.chkCenter.Enabled = false;
				return;
			case 1:
			case 3:
				this.hytbDisB.Enabled = true;
				this.hytbDisC.Enabled = false;
				this.hytbDisD.Enabled = false;
				this.hytbBoundLeftDis.Enabled = true;
				this.hytbBoundLeftWidth.Enabled = true;
				this.hytbBoundRightDis.Enabled = true;
				this.hytbBoundRightWidth.Enabled = true;
				this.chkCenter.Enabled = true;
				return;
			case 2:
				this.hytbDisB.Enabled = false;
				this.hytbDisC.Enabled = false;
				this.hytbDisD.Enabled = false;
				this.hytbBoundLeftDis.Enabled = true;
				this.hytbBoundLeftWidth.Enabled = true;
				this.hytbBoundRightDis.Enabled = true;
				this.hytbBoundRightWidth.Enabled = true;
				this.chkCenter.Enabled = true;
				return;
			default:
				return;
			}
		}

		private void btnInLineSetting_Click(object sender, EventArgs e)
		{
			GraphicsStyle lineStyleByNames = this.GetLineStyleByNames(this.cmbInLineStyle.Text);
			new LineStyleEditForm(this.cmbInLineStyle.Text, lineStyleByNames).ShowDialog();
		}

		private void btnOutLineSetting_Click(object sender, EventArgs e)
		{
			GraphicsStyle lineStyleByNames = this.GetLineStyleByNames(this.cmbOutLineStyle.Text);
			new LineStyleEditForm(this.cmbOutLineStyle.Text, lineStyleByNames).ShowDialog();
		}

		private void btnDraw_Click(object sender, EventArgs e)
		{
			if (this.hytbDisA.Text.Trim() == "" || this.hytbDisB.Text.Trim() == "" || this.hytbDisC.Text.Trim() == "" || this.hytbDisD.Text.Trim() == "" || this.hytbDisToWall.Text.Trim() == "" || this.hytbRadius.Text.Trim() == "" || this.hytbPipeDN.Text.Trim() == "")
			{
				YJKMessageBox.Information("参数不能有空！");
				return;
			}
			this.ReadUIData();
			this.SaveUIData();
			base.DialogResult = DialogResult.OK;
			base.Close();
		}

		private void btnCancel_Click(object sender, EventArgs e)
		{
			base.Close();
		}

		private void cmbPgStyle_SelectedIndexChanged(object sender, EventArgs e)
		{
			this.LoadBmps();
			this.UIContrlsEanble(this.cmbPgStyle.SelectedIndex);
		}

		private void SaveUIData()
		{
			Dictionary<string, string> dictionary = new Dictionary<string, string>();
			foreach (PropertyInfo propertyInfo in this.drawFloorPipeData.GetType().GetProperties())
			{
				if (propertyInfo.Name == "InType" || propertyInfo.Name == "OutType")
				{
					dictionary.Add(propertyInfo.Name, (propertyInfo.GetValue(this.drawFloorPipeData) as GraphicsStyle).Name.ToString());
				}
				else if (propertyInfo.Name == "PipeStyle")
				{
					dictionary.Add(propertyInfo.Name, ((int)propertyInfo.GetValue(this.drawFloorPipeData)).ToString());
				}
				else
				{
					dictionary.Add(propertyInfo.Name, propertyInfo.GetValue(this.drawFloorPipeData).ToString());
				}
			}
			dictionary.Add("LayInRoom", this.bLayInRoom.ToString());
			dictionary.Add("MarkTag", this.bMarkTag.ToString());
			dictionary.Add("BelinkSelect", this.ckbLinkSelect.Checked.ToString());
			this.userSetting.WriteFloorPipeDrawSetting(dictionary);
			int selectedIndex = this.cmbPgStyle.SelectedIndex;
			string name = Enum.GetName(typeof(ePipeType), selectedIndex);
			this.userSetting.WriteFloorPipeDrawSetting(dictionary, name.Remove(0, 1));
		}

		private void ReadUserUIData()
		{
			Dictionary<string, string> floorPipeDrawSetting = this.userSetting.GetFloorPipeDrawSetting();
			foreach (PropertyInfo propertyInfo in this.drawFloorPipeData.GetType().GetProperties())
			{
				if (!(floorPipeDrawSetting[propertyInfo.Name] == ""))
				{
					if (propertyInfo.Name == "InType" || propertyInfo.Name == "OutType")
					{
						propertyInfo.SetValue(this.drawFloorPipeData, this.GetLineStyleByNames(floorPipeDrawSetting[propertyInfo.Name]));
					}
					else if (propertyInfo.Name == "PipeStyle")
					{
						propertyInfo.SetValue(this.drawFloorPipeData, (FloorPipeStyle)int.Parse(floorPipeDrawSetting[propertyInfo.Name]));
					}
					else if (propertyInfo.Name == "PipeMaterial")
					{
						propertyInfo.SetValue(this.drawFloorPipeData, floorPipeDrawSetting[propertyInfo.Name]);
					}
					else if (propertyInfo.Name == "BDrawCenter")
					{
						propertyInfo.SetValue(this.drawFloorPipeData, bool.Parse(floorPipeDrawSetting[propertyInfo.Name]));
					}
					else
					{
						propertyInfo.SetValue(this.drawFloorPipeData, Math.Round(double.Parse(floorPipeDrawSetting[propertyInfo.Name]) * 304.8, 0));
					}
				}
			}
			this.bLayInRoom = (floorPipeDrawSetting["LayInRoom"] != "" && bool.Parse(floorPipeDrawSetting["LayInRoom"]));
			this.bMarkTag = (floorPipeDrawSetting["MarkTag"] != "" && bool.Parse(floorPipeDrawSetting["MarkTag"]));
			this.BeLinkSelect = (floorPipeDrawSetting["BelinkSelect"] != "" && bool.Parse(floorPipeDrawSetting["BelinkSelect"]));
		}

		private void btnLineStyleSetting_Click(object sender, EventArgs e)
		{
			LineStyleEditForm lineStyleEditForm;
			if (this.cmbPgStyle.SelectedIndex == 4)
			{
				lineStyleEditForm = new LineStyleEditForm(this.drawFloorPipeData.InType, this.drawFloorPipeData.OutType, this.LineStyle, true);
			}
			else
			{
				lineStyleEditForm = new LineStyleEditForm(this.drawFloorPipeData.InType, this.drawFloorPipeData.OutType, this.LineStyle, false);
			}
			if (lineStyleEditForm.ShowDialog() == DialogResult.OK)
			{
				this.drawFloorPipeData.InType = lineStyleEditForm.graphicStyleIn;
				this.drawFloorPipeData.OutType = lineStyleEditForm.graphicStyleOut;
			}
		}

		private void hytbRadius_Validating(object sender, CancelEventArgs e)
		{
			if (this.hytbRadius.Text.Trim() == "")
			{
				YJKMessageBox.Information("转角半径数值不能为空！");
				this.hytbRadius.Undo();
				this.hytbRadius.Focus();
				return;
			}
			double num = double.Parse(this.hytbPipeDN.Text);
			double num2 = double.Parse(this.hytbRadius.Text);
			if ((num2 < 8.0 * num || num2 > 11.0 * num) && DialogResult.No == YJKMessageBox.Show("根据《辐射供暖供冷技术规程》JGJ 142-2012中5.4.3的规定，塑\n料管弯曲半径不应小于管道外径的8倍，最大弯曲半径不得大于\n管道外径的11倍。是否继续使用当前转角半径数值？", MessageBoxButtons.YesNo))
			{
				this.hytbRadius.Undo();
				this.hytbRadius.Focus();
			}
		}

		private void chkCenter_CheckedChanged(object sender, EventArgs e)
		{
			this.hytbDisToWall.Enabled = (!this.chkCenter.Checked || !this.chkCenter.Enabled);
		}

		private void chkCenter_EnabledChanged(object sender, EventArgs e)
		{
			this.hytbDisToWall.Enabled = (!this.chkCenter.Checked || !this.chkCenter.Enabled);
		}

		private void hytbPipeDN_Validating(object sender, CancelEventArgs e)
		{
			if (this.hytbPipeDN.Text.Trim() == "")
			{
				YJKMessageBox.Information("管径数值不能为空！");
				this.hytbPipeDN.Undo();
				this.hytbPipeDN.Focus();
			}
		}

		private void chbLayRoom_CheckedChanged(object sender, EventArgs e)
		{
			if (this.chbLayRoom.Checked)
			{
				this.ckbLinkSelect.Enabled = true;
				return;
			}
			this.ckbLinkSelect.Enabled = false;
		}

		public DrawFloorPipeData drawFloorPipeData = new DrawFloorPipeData();

		public bool bLayInRoom;

		public bool bMarkTag;

		private string[] sPipeType = new string[]
		{
			"回转",
			"直列",
			"往复",
			"跨越",
			"单回转"
		};

		private string[] _strMaterial = new string[]
		{
			"PPR",
			"PE-X",
			"PB",
			"PE-RT"
		};

		private List<GraphicsStyle> LineStyle;

		private HVACUserSetting userSetting = new HVACUserSetting();

		private Dictionary<string, string> dic;
	}
}
